package asn.match.dao;

import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.Comment;

@Repository("comment")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class CommentDaoImpl implements CommentDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addComment(Comment comment) {
		this.sessionFactory.getCurrentSession().save(comment);
	}

	@Override
	public void updateComment(Comment comment) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(comment);
	}

	@Override
	public void deleteComment(Comment comment) {
		this.sessionFactory.getCurrentSession().delete(comment);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> findAllComments() {
		return (List<Comment>) this.sessionFactory.getCurrentSession().createQuery("from Comment comment").list();
	}

	@Override
	public Comment findCommentById(Integer commentId) {
		return (Comment) this.sessionFactory.getCurrentSession().get(Comment.class, commentId);
	}

}
